/**
 * 
 */
package com.zho.common.consumer;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

public class ConsumerManager {
    private static final Logger logger = Logger.getLogger(ConsumerManager.class);

    private static Map<String, IConsumerController> MAP_CONSUMER = new HashMap<String, IConsumerController>();

    /**
     * 
     * @param key
     * @param consumer
     * @return
     */
    public static boolean register(String key, IConsumerController consumer) {
        if (key != null && consumer != null && !MAP_CONSUMER.containsKey(key)) {
            if (logger.isDebugEnabled())
                logger.debug("register(key:" + key + ", consumer:" + consumer);
            MAP_CONSUMER.put(key, consumer);
            return true;
        }
        return false;
    }

    /**
     * 
     * @param key
     * @return
     */
    public static IConsumerController unregister(String key) {
        if (key != null) {
            IConsumerController consumer = MAP_CONSUMER.remove(key);
            if (logger.isDebugEnabled())
                logger.debug("unregister(key:" + key + ", consumer:" + consumer);
            if (consumer != null) {
                consumer.stop();
            }
            return consumer;
        }
        return null;
    }

    /**
     * 
     * @param key
     * @return
     */
    public static boolean start(String key) {
        if (key != null) {
            IConsumerController consumer = MAP_CONSUMER.get(key);
            if (logger.isDebugEnabled())
                logger.debug("start(key:" + key + ", consumer:" + consumer);
            if (consumer != null) {
                return consumer.start();
            }
        }
        return false;
    }

    /**
     * 
     */
    public static void startAll() {
        Set<String> keys = MAP_CONSUMER.keySet();
        for (String key : keys) {
            start(key);
        }
        /*
         * Collection<IConsumerController> consumers = MAP_CONSUMER.values();
         * for (IConsumerController consumer : consumers) { consumer.start(); }
         */
    }

    /**
     * 
     * @param key
     * @return
     */
    public static boolean stop(String key) {
        if (key != null) {
            IConsumerController consumer = MAP_CONSUMER.get(key);
            if (logger.isDebugEnabled())
                logger.debug("stop(key:" + key + ", consumer:" + consumer);
            if (consumer != null) {
                return consumer.stop();
            }
        }
        return false;
    }

    /**
     * 
     */
    public static void stopAll() {
        /*
         * Collection<IConsumerController> consumers = MAP_CONSUMER.values();
         * for (IConsumerController consumer : consumers) { consumer.stop(); }
         */
        Set<String> keys = MAP_CONSUMER.keySet();
        for (String key : keys) {
            stop(key);
        }
    }

    /**
     * 
     * @param key
     * @return
     */
    public static void setIdel(String key) {
        if (key != null) {
            IConsumerController consumer = MAP_CONSUMER.get(key);
            if (logger.isDebugEnabled())
                logger.debug("setIdel(key:" + key + ", consumer:" + consumer);
            if (consumer != null && consumer instanceof INotifier) {
                ((INotifier) consumer).setIdel();
            }
        }
    }

    /**
     * 
     * @return
     */
    public static boolean sendNotify(String key) {
        if (key != null) {
            IConsumerController consumer = MAP_CONSUMER.get(key);
            if (logger.isDebugEnabled())
                logger.debug("sendNotify(key:" + key + ", consumer:" + consumer);
            if (consumer != null && consumer instanceof INotifier) {
                return ((INotifier) consumer).sendNotify();
            }
        }
        return false;
    }
}
